﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BLL;
using Model;
using System.Text;

namespace OA.Controllers
{
    public class LostCardController : Controller
    {
        //
        // GET: /LostCard/
        List<MemberList> member = null;
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult LostCardMain()
        {
            memberInfoManager manger = new memberInfoManager();
            memberInfo mInfo = new memberInfo();
          //  string memberId = fc["memberId"];
           string memberId = System.Web.HttpContext.Current.Request.QueryString["memberId"];
            if (!string.IsNullOrEmpty(memberId))
            {
                mInfo = manger.GetModel(memberId);
                ViewData["memberId"] = mInfo.memberId;
                ViewData["point"] = mInfo.point;
                ViewData["IDnumber"] = mInfo.IDNumber;
                ViewData["balance"] = Convert.ToDecimal(mInfo.Balance).ToString("#0.00");
                ViewData["memberName"] = mInfo.memberName;
                return View();
            }
            if (Session["memberId"] != null)
            {
                mInfo = manger.GetModel(Session["memberId"].ToString());
                ViewData["memberId"] = mInfo.memberId;
                ViewData["point"] = mInfo.point;
                ViewData["IDnumber"] = mInfo.IDNumber;
                ViewData["balance"] = Convert.ToDecimal(mInfo.Balance).ToString("#0.00");
                ViewData["memberName"] = mInfo.memberName;
                return View();
            }
            return View();
        }
        //获取选中行信息
        [HttpPost]
        public ActionResult getSelected(FormCollection fc)
        {
            memberInfoManager manger = new memberInfoManager();
            memberInfo mInfo = new memberInfo();
            string memberId = fc["memberId"];
            if (!string.IsNullOrEmpty(memberId))
            {
                string strInm;
                mInfo = manger.GetModel(memberId);
                strInm = mInfo.memberId+',';
                strInm += mInfo.point.ToString() + ',';
                strInm += mInfo.IDNumber + ',';
                strInm += mInfo.Balance.ToString() + ',';
                strInm += mInfo.memberName;
                return Content(strInm);
            }
            return View();
        }

        //挂失
        public ActionResult LostCard( FormCollection fc)
        {
            string memberId = fc["memberId"].ToString();
            int select = Convert.ToInt32(fc["selectValue"]);
            memberInfoManager manager = new memberInfoManager();
            string txt = manager.UpdateStatus(memberId, select);
            return Content(txt);
        }

        //挂失或停用的会员信息
        public ActionResult LoadData(FormCollection fc)
        {
            memberInfoManager manager = new memberInfoManager();
            List<MemberList> member = new List<MemberList>();
            int pageSize = Convert.ToInt32(fc["pageSize"]);
            member= manager.getAllNo().ToList();
            StringBuilder sb = new StringBuilder();
            sb.Append("<tr class=\"DGHeaderStyle\">");

            sb.Append("<th> 会员编号</th>");
            sb.Append("<th>会员姓名</th>");
            sb.Append("<th>会员性别</th>");
            sb.Append("<th>手机号码 </th>");
            sb.Append("<th>联系电话</th>");
            sb.Append("<th>Email</th>");
            sb.Append("<th>证件号码</th>");
            sb.Append("<th>生日</th>");
            sb.Append("<th>卡类型</th>");
            sb.Append("<th>卡内余额</th>");
            sb.Append("<th>积分</th>");
            sb.Append(" <th>卡状态</th>");
            sb.Append("<th>登记日期</th>");
            sb.Append("<th>截止日期</th>");
            sb.Append("<th>消费总额</th>");
            sb.Append("<th>备注</th>");
            sb.Append("<th>婚姻</th>");
            sb.Append("<th>邮编</th>");
            sb.Append("<th>住址</th>");
            sb.Append("<th>区域</th>");
            sb.Append("<th>会员类型</th>");
            sb.Append("<th>推介人</th>");
            sb.Append("</tr>");
            for (int i = 0; i < member.Count; i++)
            {
                if (i < pageSize)
                {
                    sb.Append("<tr>");
                    sb.Append("<td>" + member[i].memberID + "</td>");
                    sb.Append("<td><a href=\"javascript:\" onclick=\"getSelectInm("+member[i].memberID+")\">" + member[i].memberName + "</a></td>");
                    sb.Append("<td>" + member[i].sex + "</td>");
                    sb.Append("<td>" + member[i].mobile + "</td>");
                    sb.Append("<td>" + member[i].Tel + "</td>");
                    sb.Append("<td>" + member[i].Email + "</td>");
                    sb.Append("<td>" + member[i].IDNumber + "</td>");
                    sb.Append("<td>" + member[i].birthDay + "</td>");
                    sb.Append("<td>" + member[i].cardType + "</td>");
                    sb.Append("<td>" + member[i].Balance + "</td>");
                    sb.Append("<td>" + member[i].point + "</td>");
                    sb.Append("<td>" + member[i].status + "</td>");
                    sb.Append("<td>" + member[i].createDate + "</td>");
                    sb.Append("<td>" + member[i].expiresDate + "</td>");
                    sb.Append("<td>" + member[i].consume + "</td>");
                    sb.Append("<td>" + member[i].memo + "</td>");
                    sb.Append("<td>" + member[i].Marriage + "</td>");
                    sb.Append("<td>" + member[i].Zip + "</td>");
                    sb.Append("<td>" + member[i].Address + "</td>");
                    sb.Append("<td>" + member[i].areaID + "</td>");
                    sb.Append("<td>" + member[i].MemberType + "</td>");
                    sb.Append("<td>" + member[i].tuiguangren + "</td>");
                    sb.Append("</tr>");
                }
                else
                {
                    break;
                }
            }
            return Content(sb.ToString());
        }

        //实现分页
        public ActionResult newPage(FormCollection fc)
        {
            int pageNum = Convert.ToInt32(fc["pageNum"]);
            int pageSize = Convert.ToInt32(fc["pageSize"]);
            int maxPage = pageNum * pageSize - 1;
            int minPage = maxPage - pageSize + 1;
            memberInfoManager mb = new memberInfoManager();
            member = mb.getAllNo().ToList(); ;
            StringBuilder sb = new StringBuilder();
            sb.Append("<tr class=\"DGHeaderStyle\">");

            sb.Append("<th> 会员编号</th>");
            sb.Append("<th>会员姓名</th>");
            sb.Append("<th>会员性别</th>");
            sb.Append("<th>手机号码 </th>");
            sb.Append("<th>联系电话</th>");
            sb.Append("<th>Email</th>");
            sb.Append("<th>证件号码</th>");
            sb.Append("<th>生日</th>");
            sb.Append("<th>卡类型</th>");
            sb.Append("<th>卡内余额</th>");
            sb.Append("<th>积分</th>");
            sb.Append(" <th>卡状态</th>");
            sb.Append("<th>登记日期</th>");
            sb.Append("<th>截止日期</th>");
            sb.Append("<th>消费总额</th>");
            sb.Append("<th>备注</th>");
            sb.Append("<th>婚姻</th>");
            sb.Append("<th>邮编</th>");
            sb.Append("<th>住址</th>");
            sb.Append("<th>区域</th>");
            sb.Append("<th>会员类型</th>");
            sb.Append("<th>推介人</th>");
            sb.Append("</tr>");
            for (int i = minPage; i <= maxPage; i++)
            {
                try
                {
                    sb.Append("<tr onmouseover=\"this.style.backgroundColor='#BCD2EE';cursor='default'\" onmouseout=\"this.style.backgroundColor='white'\">");
                    sb.Append("<td>" + member[i].memberID + "</td>");
                    sb.Append("<td><a href=\"javascript:\" onclick=\"getSelectInm(" + member[i].memberID + ")\">" + member[i].memberName + "</a></td>");
                    sb.Append("<td>" + member[i].sex + "</td>");
                    sb.Append("<td>" + member[i].mobile + "</td>");
                    sb.Append("<td>" + member[i].Tel + "</td>");
                    sb.Append("<td>" + member[i].Email + "</td>");
                    sb.Append("<td>" + member[i].IDNumber + "</td>");
                    sb.Append("<td>" + Convert.ToDateTime(member[i].birthDay).ToString("yyyy-MM-dd") + "</td>");
                    sb.Append("<td>" + member[i].cardType + "</td>");
                    sb.Append("<td>" + Convert.ToDecimal(member[i].Balance).ToString("#0.00") + "</td>");
                    sb.Append("<td>" + member[i].point + "</td>");
                    sb.Append("<td>" + member[i].status + "</td>");
                    sb.Append("<td>" + Convert.ToDateTime(member[i].createDate).ToString("yyyy-MM-dd") + "</td>");
                    sb.Append("<td>" + Convert.ToDateTime(member[i].expiresDate).ToString("yyyy-MM-dd") + "</td>");
                    sb.Append("<td>" + Convert.ToDecimal(member[i].consume).ToString("#0.00") + "</td>");
                    sb.Append("<td>" + member[i].memo + "</td>");
                    sb.Append("<td>" + member[i].Marriage + "</td>");
                    sb.Append("<td>" + member[i].Zip + "</td>");
                    sb.Append("<td>" + member[i].Address + "</td>");
                    sb.Append("<td>" + member[i].areaID + "</td>");
                    sb.Append("<td>" + member[i].MemberType + "</td>");
                    sb.Append("<td>" + member[i].tuiguangren + "</td>");
                    sb.Append("</tr>");
                }
                catch (Exception)
                {

                    break;
                }

            }
            return Content(sb.ToString());
        }

        //最大页数
        public ActionResult Data_Load_maxPage(FormCollection fc)
        {
            int pageSize = Convert.ToInt32(fc["pageSize"]);
            memberInfoManager mb = new memberInfoManager();
            //memberInfo mInfo = new memberInfo();
            int x = Convert.ToInt32(fc["balance"]);
            member = mb.getAllNo().ToList();
            int count = member.Count();
            string oop = Math.Ceiling((double)count / pageSize).ToString();
            return Content(oop);
        }

        public ActionResult exchangCard(FormCollection fc)
        {
            string memberId = fc["memberId"];
            memberInfo MI= new memberInfo();
            memberInfoManager MIManager = new memberInfoManager();
            MI = MIManager.GetModel(memberId);
            if (MI != null)
            {
                string memberCardId = DateTime.Now.ToString("yyyyMMddhhmmssff").Trim();
                MI.memberCardId = memberCardId;
                MIManager.Update();
                return Content(memberCardId);
            }
            else
            {
                return Content("未找到该会员,换卡失败");
            }
        }
    }
}
